package com.yukino.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yukino.mapper.DeptMapper;
import com.yukino.mapper.MemberMapper;
import com.yukino.mapper.UserMapper;
import com.yukino.pojo.Dept;
import com.yukino.pojo.Project;
import com.yukino.pojo.User;
import com.yukino.pojo.requestBody.UserProfile;
import com.yukino.pojo.serviceResult.LoginUser;
import com.yukino.pojo.requestBody.RegisterBody;
import com.yukino.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 判断用户名是否存在
     * 存在 false 不存在 true
     *
     * @param username 用户名
     */
    @Override
    public boolean queryUsernameExit(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        return user == null;
    }


    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     */
    @Override
    public LoginUser login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);

        User user = userMapper.selectOne(wrapper);

        String pass = SaSecureUtil.sha256(password);

        if (queryUsernameExit(username)) {
            return LoginUser.getNoExit();
        } else if (!pass.equals(user.getPassword())) {
            return LoginUser.getError();
        } else {
            return LoginUser.getSuccess(user);
        }
    }

    /**
     * 注册
     *
     * @param register 注册的信息
     */
    @Override
    @Transactional
    public User createUser(RegisterBody register) {
        User user = new User();
        // 加密数据
        String password = SaSecureUtil.sha256(register.getPassword());

        user.setUsername(register.getUsername());
        user.setPassword(password);
        user.setPhone(register.getPhone());

        userMapper.insert(user);
        return user;
    }

    /**
     * 根据账号查询
     * sa-token框架中，本次实验选择通过username字段判断唯一
     * @param username
     */
    @Override
    public Long queryUserId(String username) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        return user.getUser_id();
    }

    /**
     * 查询用户参与的项目
     * @param username 账号
     */
    @Override
    public List<Project> querySpecificProject(String username) {
        Long user_id = queryUserId(username);
        List<Project> projects = userMapper.selectProjectInfo(user_id);
        return projects;
    }

    @Override
    public User queryInfo(String username) {
        return userMapper.selectDeptInfo(username);
    }

    /**
     * 更新密码
     * @param username 账号
     * @param password 密码
     */
    @Override
    public int updatePwd(String username, String password) {
        String s = SaSecureUtil.sha256(password);

        User user = new User();
        user.setPassword(s);

        return userMapper.update(user, new QueryWrapper<User>().eq("username", username));
    }

    /**
     * 更新用户信息
     * @param username 账号
     * @param userProfile 用户信息
     * @return
     */
    @Override
    public int updateProfile(String username, UserProfile userProfile) {
        User user = new User();
        user.setName(userProfile.getName());
        user.setSex(userProfile.getSex());

        userMapper.updateDept_id(username,userProfile.getDept_id());

        return userMapper.update(user, new QueryWrapper<User>().eq("username", username));
    }
}
